# Create your views here.
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.http import HttpResponse
from django.core.paginator import Paginator
from myhomework.account.daos import *
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login
from myhomework.exceptions.exception import *
from myhomework.account.decorators import *
from myhomework.common.decorators import *
from myhomework.common.daos import *
from myhomework.common.forms import *
from django.template import RequestContext

class CommonView(object):
    
    @staticmethod
    @login_required
    @user_profile
    def index(request,context={}):
        return render_to_response('index.html',context, context_instance=RequestContext(request))
    
    @staticmethod
    @login_required
    @user_profile
    @process_exception
    def edit_settings(request,context={}):
        if request.method == 'POST':          
            form = EditSettingsForm(request.POST)
            if form.is_valid():
                new_settings = form.save(commit=False)
                SettingDao.update_settings(new_settings)
                #cd = form.cleaned_data
                #UserDao.user_set_password(request.user.username,cd['old_password'],cd['new_password'],cd['new_password'])
                return HttpResponse('{}')
            else:
                raise UserDataFormError(form.errors)
        context.update({'settings':SettingDao.get_settings()})
        return render_to_response('ajax/edit_settings.html',context, context_instance=RequestContext(request))
    
    @staticmethod
    @login_required
    @user_profile
    def get_template(request,context={}):
        return render_to_response(request.GET['template'],context, context_instance=RequestContext(request))